import GoodStore from './GoodStore'
import VendorStore from './VendorStore'

/**
 * Card 数据仓库
 * */
export class CardStore {
  /** 店铺数据对象集合 */
  private vendorMap: Record<string, VendorStore> = {}

  /** 商品数据对象集合 */
  private goodMap: Record<string, GoodStore> = {}

  /** 获取一个 vendor */
  getVendor(id: string) {
    return this.vendorMap[id]
  }

  /** 增加一个 vendor */
  setVendor(id: string, vendor: IVendorObj) {
    this.vendorMap[id] = new VendorStore(vendor)
  }

  /** 获取一个 good */
  getGood(id: string) {
    return this.goodMap[id]
  }

  /** 增加一个 good */
  setGood(goodId: string, good: ISkuObj, vendorId: string, vendor: IVendorObj) {
    const vendorStore = this.vendorMap[vendorId]
    if (vendorStore) {
      this.goodMap[goodId] = new GoodStore(good, vendorStore)
    } else {
      // vendor 没有增加上，正常不会出现这个错误
      const vendorStore = new VendorStore(vendor)
      this.vendorMap[vendorId] = vendorStore
      this.goodMap[goodId] = new GoodStore(good, vendorStore)
    }
  }

  /** 清空 */
  clear() {
    this.vendorMap = {}
    this.goodMap = {}
  }
}

export const cardStore = new CardStore()
